package ie.timalb.chess.main.core.movement;

import ie.timalb.chess.main.core.exceptions.ImpossibleToTraceARouteException;
import ie.timalb.chess.main.core.exceptions.SquareOutOfRangeException;
import ie.timalb.chess.main.core.pieces.Piece;
import ie.timalb.chess.main.core.pieces.Piece.Color;
import ie.timalb.chess.main.core.squares.Square;
import ie.timalb.chess.main.core.squares.SquareWithPiece;
import ie.timalb.chess.main.core.temp.SquareIterator;
import ie.timalb.chess.main.core.temp.Squares;

public class Movement extends Route{
	public Movement(Square sourceSquare, Square destinySquare)
			throws ImpossibleToTraceARouteException, SquareOutOfRangeException {
		super(sourceSquare, destinySquare);
	}

	public int getDeltaXBasedOnColor ()
	{
		return getDeltaX();
	}
	
	public int getDeltaYBasedOnColor ()
	{		
		if (getPiece().getColor() == Color.BLACK)
		{
			return -getDeltaY();
		}
		return getDeltaY();
	}
	
	public int getRouteStepsBasedOnColor() throws ImpossibleToTraceARouteException {
		if (getPiece().getColor() == Color.BLACK)
		{
			return -getRouteSteps();
		}
		return getRouteSteps();
	}	

	public Square getDestinySquare() {
		return destinySquare;
	}	
	
	public Piece getPiece() {
		return this.getSourceSquare().getContent();
	}
	
	@Override
	public SquareWithPiece getSourceSquare() {
		// TODO Auto-generated method stub
		return (SquareWithPiece) super.getSourceSquare();
	}

	public SquareIterator getIterator(boolean includeSrc, boolean includeDestiny){
		Squares toIterate = new Squares(getBoard(), getSquares());
		Square startSquare;
		Square finalSquare;
		
		if (includeSrc){
			toIterate.add(getSourceSquare());
			startSquare = getSourceSquare();
		}else{
			try {
				startSquare = getBoard().getNextSquare(getSourceSquare(), getDirection());
			} catch (SquareOutOfRangeException e) {
				return (null);
			}
		}
		
		if (includeDestiny){
			toIterate.add(getDestinySquare());
			finalSquare = getDestinySquare();
		}else
		{
			try {
				finalSquare = getBoard().getPreviousSquare(getDestinySquare(), getDirection());
			} catch (SquareOutOfRangeException e) {
				return (null);
			}
		}
		return new SquareIterator (toIterate, getDirection(), startSquare, finalSquare);
	}
}
